package demo2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

@SuppressWarnings({"all"})
public class TcpEchoClient {
    private Socket socket = null;
    //我们需要在进行这个socket创建的时候和我们的服务器建立连接
    public TcpEchoClient(String serverIp, int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }

    public void start() throws IOException {
        //控制台读取输入的内容
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream()){
            PrintWriter printWriter = new PrintWriter(outputStream);
            Scanner scannernet = new Scanner(System.in);
            while(true){
                System.out.println("->");
                String request=scanner.next();
                /**
                 * //字符串作为请求发送给我们的服务器
                 *                 //从服务器读取相应
                 *                 //把这个响应打印出来
                 */
                printWriter.println(request);
                printWriter.flush();
                //下面的这个就是响应
                String response=scannernet.next();
                System.out.println(response);
            }
        }catch(IOException e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("192.168.190.1",9090);
        tcpEchoClient.start();
    }
}
